package cn.tanghom.support.utils;

import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

import java.util.Random;

/**
 * 描述
 *
 * @author tanghom<tanghom@qq.com> on 2016/5/26
 */
public class AliDaYu {


    private static String sendSms(String mobile, String smsString,String signName, String smsTemplateCode) {
        try {
            String url = PropertiesUtils.findPropertiesKey("alidayu_url");
            String appkey = PropertiesUtils.findPropertiesKey("alidayu_appkey");
            String secret = PropertiesUtils.findPropertiesKey("alidayu_secret");
            TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
            AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
            req.setExtend("extend");
            req.setSmsType("normal");
            req.setSmsFreeSignName(signName);
            req.setSmsParamString(smsString);
            req.setRecNum(mobile);
            req.setSmsTemplateCode(smsTemplateCode);
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            System.out.println(rsp.getBody());
        } catch (Exception e) {
            LoggerUtils.error(e);
        }
        return "success";
    }

    /**
     * 发送注册短信
     * @param mobile
     * @return
     */
    public static String reg(String mobile) {
        try{
            Random random = new Random();
            String code = random.nextInt(9000) + 1000 + "";
            String signName = "大鱼测试";
            String product = "开发测试应用";
            String smsTemplateCode = PropertiesUtils.findPropertiesKey("alidayu_reg_code");
            String smsString = "{\"code\":\"" + code + "\",\"product\":\"" + product + "\"}";
            AliDaYu.sendSms(mobile, smsString, signName,smsTemplateCode);
            return code;
        } catch (Exception e) {
            return "";
        }
    }
    /**
     * 发送注册短信
     * @param mobile
     * @return
     */
    public static String findPwd(String mobile) {
        try{
            Random random = new Random();
            String code = random.nextInt(9000) + 1000 + "";
            String signName = "大鱼测试";
            String product = "开发测试应用";
            String smsTemplateCode = PropertiesUtils.findPropertiesKey("alidayu_find_pwd_code");
            String smsString = "{\"code\":\"" + code + "\",\"product\":\"" + product + "\"}";
            AliDaYu.sendSms(mobile, smsString, signName,smsTemplateCode);
            return code;
        } catch (Exception e) {
            return "";
        }
    }
    /**
     * 发送短信
     * @param mobile
     * @param content
     * @return
     */
    public static String sms(String mobile,String content) {
        try{
            Random random = new Random();
            String code = random.nextInt(9000) + 1000 + "";
            String signName = "大鱼测试";
            String product = "开发测试应用";
            String smsTemplateCode = PropertiesUtils.findPropertiesKey("alidayu_find_pwd_code");
            String smsString = "{\"code\":\"" + code + "\",\"product\":\"" + product + "\"}";
            AliDaYu.sendSms(mobile, smsString, signName,smsTemplateCode);
            return code;
        } catch (Exception e) {
            return "";
        }
    }
    public static void main(String[] args) {
        AliDaYu.findPwd("18613236735");
    }
}
